// 网关配置的前缀
var companyServer = "/companySer"

// 文本框中的值,以及判断是否输入正确的标志位
var duty_value = "", duty_correct = false;
var name_value = "", name_correct = false;
var email_value = "";
var phone_value = "";
account_correct = false;// 用户名输入是否正确

// let companyId = "1354255569179971586";
let companyId = "";

let Authorization = "";
let refreshToken = "";

let UUID = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "createHR");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    UUID = createUUID();
    $("#uuid").val(UUID);
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    companyId = $("#companyId").html();
    // console.log(companyId)
}

// 上传照片 部分
// 图片地址
var photo_path = "";
$("input[name='photo']").change(function (event) {
    // 获取图片元素
    var ele = event.target;

    if (!/.(gif|jpg|jpeg|png|GIF|JPG|bmp)$/.test(ele.value)) {
        alert("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");
        return false;
    }

    var files = ele.files;
    if (files && files.length <= 0) {
        alert("出错了，请重新上传。")
    }

    var file = files[0];
    if (file.size > 1024 * 100) {
        alert('照片大小不能超过 100KB!');
        return false;
    }

    // 获取目前上传的文件
    console.log("上传了照片");

    var path = URL.createObjectURL(file);
    photo_path = path;
    $(".create-photo-left").css("background-image", "url(" + path + ")")
        .css("background-repeat", "no-repeat")
        .css('background-size', '100% 100%');
    $("#photo-logo *").attr("hidden", "hidden");


    // 将文件传到后端
    $('#uploadPhoto').ajaxSubmit({
        datatype: 'json',
        success: function (res) {
            cocoMessage.info(2000, "图片上传成功", function () {
            });
        },
        error: function (res) {
            cocoMessage.error(2000, "图片上传出错，请重试", function () {
            });
        }
    });
});

// 提交修改
// 修改提交按钮状态
// isSubmit : 是否 可以 提交
function change_but_status(isSubmit) {
    if (isSubmit) {// 可以提交
        $("#submit").attr('disabled', false);
        $("#submit").css('background-color', '#1dae0e');
        $(".tips").attr('hidden', true);
    } else {// 不可提交
        $("#submit").attr('disabled', true);
        $("#submit").css('background-color', '#808080c2');
        $(".tips").attr('hidden', false);
    }
}

// 判断文本框中的值是否修改，修改了则可以提交

$('#duty').change(function () {
    duty_value = $(this).val();
    if (duty_value == null || duty_value == "") {
        $('#error_duty').html("职责输入不能为空");
        duty_correct = false;
    } else {
        $('#error_duty').html("");
        duty_correct = true;
    }
    change_but_status(duty_correct
        && name_correct
        && account_correct);
});

$('#name').change(function () {
    name_value = $(this).val();
    if (name_value == null || name_value == "") {
        $('#error_name').html("姓名输入不能为空");
        name_correct = false;
    } else {
        $('#error_name').html("");
        name_correct = true;
    }
    change_but_status(duty_correct
        && name_correct
        && account_correct);
});

$('.account').change(function () {
    email_value = $('#email').val();
    phone_value = $('#phone').val();

    if ((email_value == null || email_value == "")
        && (phone_value == null || phone_value == "")) {
        $('#error_email').html("两者不能同时为空");
        $('#error_phone').html("两者不能同时为空");
        account_correct = false;
    } else {
        $('#error_email').html("");
        $('#error_phone').html("");
        account_correct = true;
    }
    change_but_status(duty_correct
        && name_correct
        && account_correct);
});

function crateHr() {

    $.ajax({
        type: "PUT",
        async: false,
        url: companyServer + "/company/hrInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "duty": duty_value,
            "email": email_value,
            "phone": phone_value,
            "companyId": companyId,
            "uuid": UUID,
        }),
        success: function (data) {
            unauthorized(data);
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });

                // 提交完后，重置初始值
                $('#phone').val("");
                $('#email').val("");
                $('#name').val("");
                $('#duty').val("");
                $("input[name='photo']").val("");
                $(".create-photo-left").css("background", "#99999969");
                $("#photo-logo *").removeAttr("hidden");

                // 提交完后，设置按钮为不可提交
                change_but_status(false);

            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function random () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
function createUUID() {
    return (random() + random() + "-" + random() + "-" + random() + "-" + random() + random());
};
